<?php
header('content-type:text/html; charset=utf-8');


$SERVER = 'localhost:3306';
$username = $password = 'root';
$dbname = 'mall123';
$conn = mysqli_connect($SERVER, $username, $password, $dbname);

if (!$conn) {
    die('数据库连接失败');
}
;
header('Access-Control-Allow-Origin: *');  
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
// echo $username;
// echo $password;

// 注册
$sql = "SELECT * from myinfo where username = '$username'";
// 执行数据库的查询
$result = mysqli_query($conn, $sql);
// 获取结果中的值
$data = mysqli_fetch_assoc($result);
// 判断用户名是否存在
if ($data) {
    echo json_encode(array(
        'code' => 0,
        'msg' => '用户名已存在',
    ));

} else {
    $sql = "INSERT INTO myinfo (username, password) VALUES ('$username', '$password')";

    mysqli_query($conn, $sql);//受影响行数

    $rows = mysqli_affected_rows($conn);//受影响行数

    if ($rows > 0) {
        echo json_encode(array(
            'code' => 1,
            'msg' => '注册成功',
        ));
    }
}
